package org.sky.framework.security;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.sky.framework.entity.AuthProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

public abstract class AbstractSecurityController {
	protected static Logger logger = LoggerFactory.getLogger(AbstractSecurityController.class);
	@Value(value = "${security.server.application.name}")
	protected String securityServerId;

	@Autowired
	protected AuthProperties authProperties;

	@Autowired
	protected RestTemplate restTemplate;

	@Autowired
	protected DigestSignature digestSignature;

	/**
	 * 登录
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	protected abstract AuthEntity login(String username, String password);

	/**
	 * 注销
	 * 
	 * @param userid
	 * @return
	 */
	protected abstract boolean logout(String username);

	/**
	 * 获取token
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/security/login", method = { RequestMethod.POST, RequestMethod.GET })
	public AuthEntity login(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("username") String username, @RequestParam("password") String password) {
		return login(username, password);
	}

	/**
	 * 刷新token
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/security/logout", method = { RequestMethod.POST, RequestMethod.GET })
	public boolean logout(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("username") String username) {
		return logout(username);
	}

}
